#pragma once
#include"HastTable.h"


namespace ns_unordered_map_set{
	template<class K, class V>
	class unordered_map {
	public:
		struct MapKeyOfT {
			const K& operator()(const pair<K, V>& kv) {
				return kv.first;
			}
		};

		typedef typename OpenHash::HashTable<K, pair<K, V>, MapKeyOfT>::iterator iterator;

		iterator begin() {  
			return _ht.begin();
		}

		iterator end() {
			return _ht.end();
		}

		pair<iterator, bool> insert(const pair<K, V>& kv) {
			return _ht.Insert(kv);
		}

		void erase(const K& key)
		{
			_ht.Erase(key);
		}

		iterator find(const K& key)
		{
			return _ht.Find(key);
		}

		V& operator[](const K& key) {
			pair<iterator, bool> ret = _ht.Insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		OpenHash::HashTable<K, pair<K, V>, MapKeyOfT> _ht;
	};

	void test_unordered_map() {
		unordered_map<string, string> dict;
		dict.insert(make_pair("sort", "排序"));
		dict["left"] = "左边";
		dict["left"] = "剩余";
		dict["string"] = "字符串";
		dict["right"] = "右边";

		unordered_map<string, string>::iterator it = dict.begin();
		while (it != dict.end()) {
			cout << it->first << " : " << it->second << endl;
			++it;
		}
		cout << endl;
	}
}
